<?php
/**
 * Permission Service
 * User: zng
 * Date: 2019/11/4
 * Time: 14:34
 */
namespace app\admin\service\foods;

use app\admin\model\foods\Foods as FoodsModels;
use think\Db;
use think\db\Query;

Class Foods {

    /**
     * 首页列表
     * @param array $get 筛选条件
     * @author zx
     * @date 2020-03-17
     * @return \think\Paginator
     */
    public function indexPaginate( array $get=[] )
    {
        $map = [];
        if ( get($get, 'title') ) {
            $map[] = ['title', 'LIKE', $get['title'].'%'];
        }
        if ( get($get, 'status') === 0 || get($get, 'status') === '0' || get($get, 'status') == 1 ) {
            $map[] = ['status', '=', $get['status']];
        }
        if ( get($get, 'start_sort_num') !== NULL && get($get, 'start_sort_num') !== '' ) {
            $map[] = ['sort_num', '>=', $get['start_sort_num']];
        }
        if ( get($get, 'end_sort_num') !== NULL && get($get, 'end_sort_num') !== '' ) {
            $map[] = ['sort_num', '<=', $get['end_sort_num']];
        }
        $pageSize = get($get,'pageSize')>0?$get['pageSize']:20;
        return FoodsModels::where($map)
            ->order('id', 'desc')
            ->paginate($pageSize);
    }

    /**
     * 状态更改
     * @param Foods $model
     * @author zx
     * @date 2020-03-17
     * @return bool
     */
    public function status(FoodsModels $model)
    {
        $status = (int)(!$model->status);
        $model->status = $status;
        return $model->save();
    }

    /**
     * 新增\编辑信息
     * @param array $data 数据包
     * @author zx
     * @date 2020-03-17
     * @return boolean|integer
     */
    public function create(array $data)
    {
        $foodCategory = FoodsModels::create($data, ['title','desc','img_url','original_price','pack_fee','price','sort_num','status','stock']);
        return $foodCategory->isEmpty()?false:$foodCategory->id;
    }

    /**
     * 关联新增
     * @param int $fid
     * @param array $cid
     * @return mixed
     */
    public function relatedCreate(int $fid, array $cid)
    {
        return FoodsModels::get($fid)->category()->saveAll($cid);
    }

    /**
     * 关联删除
     * @param int $fid
     * @return mixed
     */
    public function relatedDelete(int $fid)
    {
        return FoodsModels::get($fid)->category()->detach();
    }

    /**
     * 批量删除
     * @param mixed $data
     * @author zx
     * @date 2020-03-17
     * @return boolean
     */
    public function destory( $data )
    {
        Db::startTrans();
        if ( is_array( $data ) ) {
            foreach ( $data as $_data ) {
                if ( ! $this->relatedDelete($_data) ) {
                    Db::rollback();
                    return false;
                }
            }
        } else {
            if ( ! $this->relatedDelete($data) ) {
                Db::rollback();
                return false;
            }
        }
        if (false === FoodsModels::destroy(function(Query $query)use($data){
            $where = ['id', '=', $data];
            if ( is_array($data) ) {
                $where = ['id', 'in', $data];
            }
            $query->where([
                $where
            ]);
        })) {
            Db::rollback();
            return false;
        }
        Db::commit();
        return true;
    }

    /**
     * 菜品分类编辑
     * @param FoodsModels $model
     * @param array $data
     */
    public function save(FoodsModels $model, array $data)
    {
        $model->title = $data['title'];
        $model->sort_num = $data['sort_num'];
        $model->status = $data['status'];
        $model->desc = $data['desc'];
        $model->stock = $data['stock'];
        $model->img_url = $data['img_url'];
        $model->original_price = $data['original_price'];
        $model->price = $data['price'];
        $model->pack_fee = $data['pack_fee'];
        return $model->save();
    }


}